package com.example.shirospringboot.controller.clientController;

import com.example.shirospringboot.pojo.Place;
import com.example.shirospringboot.service.placeService.PlaceServiceImpl;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sun.istack.internal.NotNull;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class placeController {


    @Autowired
    PlaceServiceImpl placeService;

    @RequestMapping("/listPlace")
    public String showPlace(HttpSession session,Model model){
        int clientId = (int) session.getAttribute("clientId");
        System.out.println(clientId);
        List<Place> places = placeService.queryAllPlaceByClientId(clientId);

        model.addAttribute("Places",places);
        return "emp/list";
    }

    @RequestMapping("/updatePlace")
    public String updatePlace(Place place) throws ParseException {

        System.out.println(place);

        System.out.println(place.getPlaceStatus());

        if(place.getPlaceStatus().equals("空闲")||place.getPlaceStatus().equals("不可用")){

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date d = sdf.parse("1990-01-01");
            place.setUseStartTime(d);

        }

        System.out.println(place);
        placeService.updatePlaceById(place);
        return "redirect:/listPlace";
    }

    @RequestMapping("/toUpdatePlace")
    @ResponseBody
    public Map toUpdatePlace(Integer id) {
        Map<String,Object> map = new HashMap<>();

        Place place = placeService.queryPlaceById(1);
        map.put("updatePlace",place);

        return map;
    }

}
